
import axios from '@/utils/http';

const SET_ARTICLE_LIST = 'SET_ARTICLE_LIST';
const SET_ARTICLE_DETAIL = 'SET_ARTICLE_DETAIL';
const SET_COMMENT_LIST = 'SET_COMMENT_LIST';

const initState = {
  articleList: [],
  articleCount: 0,
  articleDetail: {
    tags: [],
    pictures: [],
    creator: {},
  },
  commentList: [],
  commentCount: 0,
};

const getters = {};
const actions = {
  getArticleList(ctx, { tags = '', nickname = '', start = 0, limit = 10 } = {}) {
    return axios.get('/social/article/list', {
      params: {
        tags,
        nickname,
        start,
        limit,
      },
    })
      .then(({ data }) => {
        ctx.commit(SET_ARTICLE_LIST, data);
      });
  },
  getArticleDetail(ctx, { id }) {
    return axios.get(`/social/article/${id}`)
      .then(({ data }) => {
        ctx.commit(SET_ARTICLE_DETAIL, data);
      });
  },
  getCommentList(ctx, { articleId, start = 0, limit = 10 }) {
    return axios.get('/social/comment', {
      params: {
        articleId,
        start,
        limit,
      },
    })
      .then(({ data }) => {
        ctx.commit(SET_COMMENT_LIST, data);
      });
  },

  like(ctx, { articleId }) {
    return axios.post('/social/like', { articleId });
  },
};

const mutations = {
  [SET_ARTICLE_LIST](state, deploy) {
    state.articleList = deploy.list;
    state.articleCount = deploy.count;
  },
  [SET_COMMENT_LIST](state, deploy) {
    state.commentList = deploy.list;
    state.commentCount = deploy.count;
  },
  [SET_ARTICLE_DETAIL](state, deploy) {
    state.articleDetail = deploy;
  },
};

export default {
  state: initState,
  getters,
  actions,
  mutations,
};
